package com.xul.controller;

import com.xul.entity.Clazz;
import com.xul.entity.Student;
import com.xul.entity.Subject;
import com.xul.entity.Teacher;
import com.xul.service.ClazzService;
import com.xul.service.StudentService;
import com.xul.service.StudentService;
import com.xul.service.SubjectService;
import com.xul.utils.MapController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * @author xul
 * @create 2021-07-02 19:06
 */

@Controller
@RequestMapping("student")
public class StudentController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private SubjectService subjectService;

    @Autowired
    private ClazzService clazzService;

    private final String LIST = "student/list";
    private final String ADD = "student/add";
    private final String UPDATE = "student/update";


    @GetMapping("add")
    public String add(ModelMap modelMap) {
        List<Subject> subjects = subjectService.query(null);
        modelMap.addAttribute("subjects", subjects);
        return ADD;
    }

    @PostMapping("create")
    @ResponseBody
    public Map<String, Object> create(Student student) {
        int result = studentService.create(student);
        if (result <= 0) {
            return MapController.getInstance().error().getMap();
        }
        return MapController.getInstance().success().getMap();
    }

    @PostMapping("delete/{id}")
    @ResponseBody
    public Map<String, Object> delete(@PathVariable("id") Integer id) {
        int result = studentService.delete(id);
        if (result <= 0) {
            return MapController.getInstance().error().getMap();
        }
        return MapController.getInstance().success().getMap();
    }

    @PostMapping("delete")
    @ResponseBody
    public Map<String, Object> delete(String ids) {
        int result = studentService.delete(ids);
        if (result <= 0) {
            return MapController.getInstance().error().getMap();
        }
        return MapController.getInstance().success().getMap();
    }

    @PostMapping("update")
    @ResponseBody
    public Map<String, Object> update(@RequestBody Student student) {
        int result = studentService.update(student);
        if (result <= 0) {
            return MapController.getInstance().error().getMap();
        }
        return MapController.getInstance().success().getMap();
    }

    @GetMapping("detail/{id}")
    public String detail(@PathVariable("id") Integer id, ModelMap modelMap) {
        Student student = studentService.detail(id);
        List<Subject> subjects = subjectService.query(null);
        modelMap.addAttribute("student", student);
        modelMap.addAttribute("subjects", subjects);
        return UPDATE;
    }


    @PostMapping("query")
    @ResponseBody
    public Map<String, Object> query(@RequestBody Student student) {
        List<Student> students = studentService.query(student);
        List<Subject> subjects = subjectService.query(null);
        List<Clazz> clazzes = clazzService.query(null);
        students.forEach(entity -> {
            subjects.forEach(subject -> {
                if (entity.getSubjectId() == subject.getId().intValue()) {
                    entity.setSubject(subject);
                }
            });
            clazzes.forEach(clazz -> {
                if (entity.getClazzId() == clazz.getId().intValue()) {
                    entity.setClazz(clazz);
                }
            });

        });
        Integer count = studentService.count(student);
        return MapController.getInstance().success().put("data", students).put("count", count).getMap();
    }

    @GetMapping("list")
    public String list() {
        return LIST;
    }

    @GetMapping("teacher_student")
    public String teacher_student(ModelMap modelMap) {
        List<Clazz> clazzes = clazzService.query(null);
        List<Subject> subjects = subjectService.query(null);
        modelMap.addAttribute("clazzes", clazzes);
        modelMap.addAttribute("subjects", subjects);
        return "student/teacher_student";
    }

    @PostMapping("teacher_student")
    @ResponseBody
    public Map<String, Object> teacher_student(Integer clazzId, Integer subjectId, HttpSession session) {
        Teacher teacher = (Teacher) session.getAttribute("user");
        List<Student> students = studentService.queryStudentByTeacher(teacher.getId(), clazzId, subjectId);
        List<Subject> subjects = subjectService.query(null);
        List<Clazz> clazzes = clazzService.query(null);
        students.forEach(student -> {
            subjects.forEach(subject -> {
                if (student.getSubjectId() == subject.getId().intValue()) {
                    student.setSubject(subject);
                }
            });
            clazzes.forEach(clazz -> {
                if (student.getClazzId() == clazz.getId().intValue()) {
                    student.setClazz(clazz);
                }
            });
        });
        return MapController.getInstance().success().add("data", students).getMap();
    }


}
